<?php
class IndexController extends Zend_Controller_Action
{
    private function getDb () //初始化数据库DB
    {
        $dbAdapter = Zend_Controller_Front::getInstance()->getParam('bootstrap')
            ->getPluginResource('db')
            ->getDbAdapter();
        return $dbAdapter;
    }
    public function init ()
    { Zend_Session::start();
        /* Initialize action controller here */

    }
    public function indexAction ()
    {  
//       $a=$_GET['office'];
//       echo $a;
//       Zend_Debug::dump($a);
         // action body
    }
    public function layAction(){

       Zend_Session::start();
      
//        $captchaSession = new Zend_Session_Namespace('captcha'); 
//        $captcha = new Zend_Captcha_Image( //初始化验证码
//        array('font' => '../public/font/simsun.ttc', 'fontsize' => 12,  //设置验证码字体
//        'imgdir' => './pic/code', 'session' => $captchaSession,  //设置生成图片路径和session名称
//        'dotNoiseLevel' => 0, 'lineNoiseLevel' => 0, 'width' => 60,  //设置噪点个数和横线数图片宽度高度字数
//        'height' => 34, 'wordlen' => 4));
//         $captcha->setGcFreq(3); 
//        
//         $captcha->generate(); 
//         $captchaWord = $captcha->getWord();
//         $Imgdir = $captcha->getImgDir(); //验证码路径
//         $captchaId = $captcha->getId(); //验证码名称
//         $png = $captcha->getSuffix(); //验证码扩展名
//         $this->view->capImg = $_SESSION['capImgSrc'];
//         $this->view->captchaWord=$_SESSION['captchaWord'];
//    	 $_SESSION['capImgSrc']= $Imgdir . $captchaId . $png;
        
    	
    }
    public function hospitalAction(){
    
    
    }
    public function captchaAction(){
    	$this->_helper->layout->disableLayout();
    	Zend_Session::start();
        $captchaSession = new Zend_Session_Namespace('captcha'); 
        $captcha = new Zend_Captcha_Image( //初始化验证码
        array('font' => '../public/font/simsun.ttc', 'fontsize' => 12,  //设置验证码字体
        'imgdir' => './pic/code', 'session' => $captchaSession,  //设置生成图片路径和session名称
        'dotNoiseLevel' => 0, 'lineNoiseLevel' => 0, 'width' => 60,  //设置噪点个数和横线数图片宽度高度字数
        'height' => 34, 'wordlen' => 4));
         $captcha->setGcFreq(3); 
        
         $captcha->generate(); 
         $captchaWord = $captcha->getWord();
         $Imgdir = $captcha->getImgDir(); //验证码路径
         $captchaId = $captcha->getId(); //验证码名称
         $png = $captcha->getSuffix(); //验证码扩展名
         $_SESSION['capImgSrc']= $Imgdir . $captchaId . $png;
         $this->view->src=$_SESSION['capImgSrc'];
         $_SESSION['captchaWord']= $captchaWord;
    	$this->view->word=$_SESSION['captchaWord'];
//    	Zend_Debug::dump($_SESSION['captchaWord']);
//    	Zend_Debug::dump($_SESSION['capImgSrc']);
    }
    public function userAction ()
    {
        Zend_Session::start(); //打开session
        $captchaSession = new Zend_Session_Namespace('captcha'); //设置session
        $captcha = new Zend_Captcha_Image( //初始化验证码
        array('font' => '../public/font/1.ttf', 'fontsize' => 24,  //设置验证码字体
        'imgdir' => './pic/code', 'session' => $captchaSession,  //设置生成图片路径和session名称
        'dotNoiseLevel' => 10, 'lineNoiseLevel' => 0, 'width' => 120,  //设置噪点个数和横线数图片宽度高度字数
        'height' => 50, 'wordlen' => 4));
        $captcha->setGcFreq(3); //定期删除图片
        $captchaWord = $captcha->getWord();
        $captcha->generate(); //生成验证码
        $Imgdir = $captcha->getImgDir(); //验证码路径
        $captchaId = $captcha->getId(); //验证码名称
        $png = $captcha->getSuffix(); //验证码扩展名
        $this->view->capImg = $Imgdir . $captchaId . $png;
        
        $username = $this->_request->getParam('username');
        $password = $this->_request->getParam('password');
        $captchaPost = $this->_request->getParam('captcha');
        if (empty($captchaPost)) {
            $this->view->checkError = NULL;
        } elseif ($captchaWord != $captchaPost) {
            $this->view->checkError = "验证码错误";
        } else {
            if ($username != NULL && $password != NULL) {
                $validator = new Zend_Validate_Alnum();
                if ($validator->isValid($username)) {
                    $db = $this->getDb();
                    $authAdapter = new Zend_Auth_Adapter_DbTable($db);
                    $authAdapter->setTableName('user')
                        ->setIdentityColumn('username')
                        ->setCredentialColumn('password')
                        ->setIdentity($username)
                        ->setCredential($password);
                    $auth = Zend_Auth::getInstance();
                    $authResult = $auth->authenticate($authAdapter);
                    $result = $authResult->isValid();
                    if ($result == TRUE) {
    
                        $this->view->checkError = "";
                        $this->_redirect('/index/index');
                        
                    } else {
                        $this->view->checkError = "用户名或者密码不正确";
                    }
                } else {}
            } else {
                $this->view->checkError = "";
            }
        }
    }
    public function registerAction (
   
    
  
    )
    {$this->_helper->layout->disableLayout();}
   
    public function regactivateAction ()
    {
        $userData = $this->_request->getParams();
        $username = $userData['username'];
        $password = $userData['pass'];
        $emali = $userData['email'];
        $userID = $userData['id'];
        $telephone = $userData['telephone'];
        function checkUsername (
        $name)
        { //验证用户名
            //        if (ereg("^[0-9a-zA-Z\\_]{5,40}$", $name)){
            if (preg_match("/^\\w+$/", $name)) {
                return $name;
            } else {
                return FALSE;
            }
        }
        $validatorUsername = checkUsername($username);
        function checkPassword ($passw)
        { //验证密码
            $validatorPass = new Zend_Validate_StringLength(6, 50);
            if ($validatorPass->isValid($passw)) {
                return $passw;
            } else {
                return false;
            }
        }
        $validatorPassword = checkPassword($password);
        //Zend_Debug::dump($validatorPassword);
        function checkEmail ($userEmail)
        { //验证邮箱
            $validator = new Zend_Validate();
            $validator->addValidator(new Zend_Validate_StringLength(4, 50))->addValidator(
            new Zend_Validate_EmailAddress());
            if ($validator->isValid($userEmail)) {
                return $userEmail;
            } else {
                return false;
            }
        }
        $validatorEmail = checkEmail($emali);
        //        echo $validatorEmail;
        //        Zend_Debug::dump($validatorEmail);
        function checkUserID ($id)
        { //验证身份证
            if (preg_match("/^\\d{15,18}$/", $id)) {
                return $id;
            } else {
                return false;
            }
        }
        $validatorID = checkUserID($userID);
         //Zend_Debug::dump($validatorID);
        function checkUserTel ($telNUM)
        { //验证电话
            if (preg_match("/^\\d{8,12}$/", $telNUM)) {
                return $telNUM;
            } else {
                return false;
            }
        }
        $validatorTel = checkUserTel($telephone);
//         Zend_Debug::dump($validatorTel);
        
        //数据库插入
        if ($validatorUsername && $validatorPassword && $validatorID &&
         $validatorTel && $validatorEmail) {
         $userDb=new Application_Model_userAdd();
         $randNUM=rand(1, 500);
         $mo5Rand=md5($randNUM);
         
         $userRow=array('username'=>$validatorUsername,'password'=>$validatorPassword,'userID'=> $validatorID ,'email'=>$validatorEmail,'telephone'=>$validatorTel,'randNUM'=>$mo5Rand);
//       Zend_Debug::dump($userRow);
         $t=$userDb->insert($userRow);
         $activateURL="http://www.a.com/index/regsuccess?username=".$username."&"."rand=".$mo5Rand;
         
        $email=new Zend_Mail('utf-8');
        $stmpEmail=new Zend_Mail_Transport_Smtp('smtp.163.com',array  
                   ('name'=>'smtp.163.com','username'=>'wanghao850707@163.com','password'=>'19850707','auth'=>'login') 
        );
        $email->addTo($validatorEmail,'ninhao')
              ->setFrom('wanghao850707@163.com','wanghao')
              ->setSubject('预约挂号注册确认')
              ->setBodyText($activateURL)
              ->setBodyHtml(
              "亲爱的".$validatorUsername."：<Br />
<br />
欢迎加入预约挂号网!<br />
请点击链接完成注册：<br />
<br /><br />".$activateURL.
"<br>如果您的email程序不支持链接点击，请将上面的地址拷贝至您的浏览器(如IE)的地址栏进入预约挂号。<br />

希望您在预约挂号的体验有益和愉快!<br /><br />


- 预约挂号网<br /><br />

(这是一封自动产生的email，请勿回复。)<br />

----------------------------------------------------------------------
              ")
              ->send($stmpEmail);
         $this->view->email=$validatorEmail;
         } else {
            echo 'fdfdf';
        }
    }
    public function regsuccessAction(){
    	$username=$_GET['username'];
    	$rand=$_GET['rand'];
//    	echo $username;
//    	echo $rand;
$db=$this->getDb();
$select=$db->select();
$sql=$select->from('user','username')->where('randNUM=?',$rand)
                                     ->where('username=?',$username);
$result=$db->fetchOne($sql);   

//Zend_Debug::dump($result);
if ($result!=false){
	$table=new Application_Model_userAdd();
	$db=$table->getAdapter();
	$set=array('regactive'=>1);
	$where=$db->quoteInto('username=?', $result);
	$row=$table->update($set, $where);
	$this->view->sussecss='注册成功';
}else {$this->_redirect('/index/index') ;}                                  
    }
}

